package com.example.wsh.worddemo.Word_Fragment;


import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.wsh.worddemo.AboutDate.GetNowDate;
import com.example.wsh.worddemo.DatabaseFile.AddWordToEveryDW;
import com.example.wsh.worddemo.DatabaseFile.CursorGetAll;
import com.example.wsh.worddemo.DatabaseFile.ReturnToLibrary;
import com.example.wsh.worddemo.R;
import com.example.wsh.worddemo.Recite_Words.recite_words;
import com.jaeger.library.StatusBarUtil;


import cn.pedant.SweetAlert.SweetAlertDialog;
import mehdi.sakout.fancybuttons.FancyButton;


public class WordFragment extends Fragment {

    private Button recite_word;
    private Button DoneAllWords;
    private Button AllProgress;
    private Button NowCiku;

    private FancyButton go_to_search;

    private SharedPreferences.Editor editor;
    private SharedPreferences prefs;
    private String every_day_word;

    private String ciku;
    private String NowDate;
    private String nowciku;

    private String TodayDate;
    private String TWords;
    private String DWords;
    private String HWords;

    private TextView TodayWords;
    private TextView DoneWords;
    private TextView HavingWords;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.word_fragment,container,false);
        return view;
    }

    @Override
    public void onStart() {
        super.onStart();

        TodayDate = GetNowDate.DoIt();
        TWords = CursorGetAll.limitsql("EveryDW","Date is '"+TodayDate+"'");
        DWords = CursorGetAll.limitsql("EveryDW","Done is 1 and Date is '"+TodayDate+"'");
        HWords = CursorGetAll.limitsql("EveryDW","Done is 0 and Date is '"+TodayDate+"'");

        prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
        ciku = prefs.getString("ciku",null);
        every_day_word = prefs.getString("every_day_word",null);
        Log.d("WSHH","get ciku  "+ciku);
        Log.d("WSHH","get every_day_word  "+every_day_word);
        if (ciku == null){
            ciku = "cet4";
            editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
            editor.putString("ciku",ciku);
            editor.apply();
        }
        switch (ciku){
            case "cet4":
                nowciku = "四级";
                break;
            case "cet6":
                nowciku = "六级";
                break;
            case "kaoyan":
                nowciku = "考研";
                break;
            case "vocabulary":
                nowciku = "专八";
                break;
            case "cet4_import":
                nowciku = "专四";
                break;
            case "cet6_import":
                nowciku = "专六";
                break;
        }
        NowCiku       =   getActivity().findViewById(R.id.NowCiku);
        String S = "词库:"+nowciku;
        NowCiku.setText(S);

        TodayWords = getActivity().findViewById(R.id.TodayWords);
        TodayWords.setText(TWords);
        DoneWords = getActivity().findViewById(R.id.DoneWords);
        DoneWords.setText(DWords);
        HavingWords = getActivity().findViewById(R.id.HavingWords);
        HavingWords.setText(HWords);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        StatusBarUtil.setTranslucentForImageViewInFragment(getActivity(),null);

        NowDate = GetNowDate.DoIt();
        recite_word   =   getActivity().findViewById(R.id.recite_word);
        DoneAllWords  =   getActivity().findViewById(R.id.DoneAllWords);
        AllProgress   =   getActivity().findViewById(R.id.AllProgress);
        NowCiku       =   getActivity().findViewById(R.id.NowCiku);
        go_to_search  =   getActivity().findViewById(R.id.go_to_search);

        String sql = "Date is '"+NowDate+"'";
        try {
            if(CursorGetAll.HaveCursor("EveryDW",sql) > 0){
                Log.d("WSHH","exist");
            }
            else {
                prefs = PreferenceManager.getDefaultSharedPreferences(getContext());
                ciku = prefs.getString("ciku",null);
                every_day_word = prefs.getString("every_day_word",null);
                Log.d("WSHH","get ciku  "+ciku);
                Log.d("WSHH","get ciku  "+every_day_word);
                if (every_day_word == null){
                    every_day_word = "30";
                    editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
                    editor.putString("every_day_word",every_day_word);
                    editor.apply();
                }
                if (ciku == null){
                    ciku = "cet4";
                    editor = PreferenceManager.getDefaultSharedPreferences(getContext()).edit();
                    editor.putString("ciku",ciku);
                    editor.apply();
                }
                AddWordToEveryDW.Dosql(ciku,every_day_word);
                Log.d("WSHH","Add OK");
            }
        }catch (Exception e){
            Log.d("WSHH",e.toString());
        }
        //还原昨天没有被记忆的单词
        String resql = "Done is 0 and Date != '"+NowDate+"'";
        Cursor recursor = CursorGetAll.GetlimitedCursor("EveryDW",resql);

        if(recursor.getCount() > 0){
            ReturnToLibrary.Dosql(ciku,resql);
        }

        recite_word.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sql = "Done is 0";
                Cursor getcursor = CursorGetAll.GetlimitedCursor("EveryDW",sql);
                Log.d("WSHH",String.valueOf(getcursor.getCount()));

                if (getcursor.getCount() > 0){
                    Intent intent = new Intent(getActivity(),recite_words.class);
                    getActivity().startActivity(intent);
                }
                else {
                    SweetAlertDialog sweetAlertDialog = new SweetAlertDialog(getActivity(),SweetAlertDialog.SUCCESS_TYPE);
                    sweetAlertDialog.setTitleText("今日任务完成！");
                    sweetAlertDialog.setConfirmText("确定");
                    sweetAlertDialog.setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.cancel();
                        }
                    }).show();
                }
            }
        });

        NowCiku.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),NowLibrary.class);
                startActivity(intent);
            }
        });


        DoneAllWords.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),DoneAllWords.class);
                startActivity(intent);
            }
        });

        AllProgress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(),AllProgress.class);
                startActivity(intent);
            }
        });

        go_to_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), Inquiry.class);
                startActivity(intent);
            }
        });

    }
}
